<?php
/**
 * CONTENIDO extension pack (mpContenidoX), consists of multiple classes to
 * provide more features for Plugins/Modules.
 *
 * @package     mpContenidoX
 * @subpackage  PluginInstaller
 * @author      Murat Purc <murat@purc.de>
 * @copyright   Copyright (c) 2008-2012 Murat Purc (http://www.purc.de)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
 * @version     $Id: ContainerPhp.class.php 21 2012-07-16 21:33:14Z purcaholic $
 */


defined('CON_FRAMEWORK') or die('Illegal call');


/**
 * Plugin install file reader for PHP files.
 *
 * @package     mpContenidoX
 * @subpackage  PluginInstaller
 * @author      Murat Purc <murat@purc.de>
 */
class mpPluginInstaller_Reader_ContainerPhp extends mpPluginInstaller_Reader_Abstract
{

    /**
     * Configuration array
     * @var  array
     */
    private static $_setupCfg = null;


    /**
     * Constructor
     * @param   string  $file
     * @return  void
     */
    public function __construct($file)
    {
        if (!is_file($file)) {
            throw new mpPluginInstaller_Exception("Missing file $file");
        }
        if (self::$_setupCfg == null) {
            self::$_setupCfg = include($file);
            if (!is_array(self::$_setupCfg)) {
                self::$_setupCfg = array();
            }
        }
    }


    /**
     * Returns meta data.
     *
     * @return  mpPluginInstaller_MetaData
     */
    public function getMetaData()
    {
        if ($this->_metaData instanceof mpPluginInstaller_MetaData) {
            return $this->_metaData;
        }

        $this->_metaData = new mpPluginInstaller_MetaData();
        if (isset(self::$_setupCfg['meta']) && is_array(self::$_setupCfg['meta'])) {
            foreach (self::$_setupCfg['meta'] as $name => $value) {
                $this->_metaData->$name = (string) $value;
            }
        }

        return $this->_metaData;
    }


    /**
     * Returns dependencies
     *
     * @return  ArrayObject  Containing mpPluginInstaller_DependencyData instances
     */
    public function getDependencies()
    {
        $obj = new ArrayObject();
        if (isset(self::$_setupCfg['dependencies']) && is_array(self::$_setupCfg['dependencies'])) {
            foreach (self::$_setupCfg['dependencies'] as $pos => $item) {
                $dependency = new mpPluginInstaller_DependencyData($item['type']);
                $dependency->setIdInternal($item['idinternal']);
                $dependency->setVersion($item['version']);
                $dependency->setUrl($item['url']);
                $obj->append($dependency);
            }
        }
        return $obj;
    }

    public function getSQL($mode)
    {
        $this->_checkMode($mode);
        if (isset(self::$_setupCfg[$mode]) && isset(self::$_setupCfg[$mode]['sql'])) {
            return self::$_setupCfg[$mode]['sql'];
        }
        return null;
    }

    public function getRessources($mode)
    {
        $this->_checkMode($mode);
        if (isset(self::$_setupCfg[$mode]) && isset(self::$_setupCfg[$mode]['ressources'])) {
            return self::$_setupCfg[$mode]['ressources'];
        }
        return null;
    }

}
